package com.summer.safe.system.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.summer.safe.base.BaseEntity;

import java.util.List;

/**
 * 菜单Entity
 *
 */
public class Menu extends BaseEntity<Menu> {

    private static final long serialVersionUID = 1L;

    /**
     * 父级菜单
     */
    private Menu parent;
    /**
     *
     */
    private String parentIds; // 所有父级编号
    /**
     * 名称
     */
    private String name;
    /**
     * 菜单的描
     */
    private String describe;
    /**
     * 链接
     */
    private String href;
    /**
     * 目标（ mainFrame、_blank、_self、_parent、_top）
     */
    private String target;
    /**
     * 图标
     */
    private String icon;
    /**
     * 按钮类型
     */
    private  String type;
    /**
     * 是否在菜单中显示（0：显示；1：不显示）
     */
    private String isShow;
    /**
     * 权限标识
     */
    private String permission;


    /*
     * 用户ID
     * */
    private String userId;

    public Menu(){
        super();
        this.sort = 30;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Menu(String id) {
        super(id);
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    public Menu getParent() {
        return parent;
    }

    public void setParent(Menu parent) {
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    public String getIsShow() {
        return isShow;
    }

    public void setIsShow(String isShow) {
        this.isShow = isShow;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getParentId() {
        return parent != null && parent.getId() != null ? parent.getId() : "0";
    }

    @JsonIgnore
    public static void sortList(List<Menu> list, List<Menu> sourcelist, String parentId, boolean cascade){
        for (int i=0; i<sourcelist.size(); i++){
            Menu e = sourcelist.get(i);
            if (e.getParent()!=null && e.getParent().getId()!=null
                    && e.getParent().getId().equals(parentId)){
                list.add(e);
                if (cascade){
                    // 判断是否还有子节点, 有则继续获取子节点
                    for (int j=0; j<sourcelist.size(); j++){
                        Menu child = sourcelist.get(j);
                        if (child.getParent()!=null && child.getParent().getId()!=null
                                && child.getParent().getId().equals(e.getId())){
                            sortList(list, sourcelist, e.getId(), true);
                            break;
                        }
                    }
                }
            }
        }
    }

    @JsonIgnore
    public static String getRootId(){
        return "1";
    }
}